package com.example.mydemo02;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.example.mydemo02.databinding.ActivityMainBinding;

import java.util.Arrays;
import java.util.List;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private ActivityMainBinding binding;

    private List<Question> questions = Arrays.asList(
            new Question(R.string.question_australia,true),
            new Question(R.string.question_oceans,true),
            new Question(R.string.question_mideast,false),
            new Question(R.string.question_africa,false),
            new Question(R.string.question_americas,true),
            new Question(R.string.question_asia,true)

    );
    private int currentIndex = 0;
    private  static  final String TAG="LifecycleActivity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.d(TAG,"onCreate:被调用");
        binding = ActivityMainBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        binding.btnTrue.setOnClickListener(this);
        binding.btnFalse.setOnClickListener(this);
        binding.btnCheat.setOnClickListener(this);

        binding.btnNext.setOnClickListener(view -> {
            currentIndex = (currentIndex + 1) % questions.size();
            updateQuestion();

        });
        updateQuestion();
        binding.tvAus.setText(questions.get(currentIndex).getTextResId());
    }

    @Override
    protected void onStart(){
        super.onStart();
        Log.d(TAG,"onStart:被调用");
    }

    @Override
    protected void onResume(){
        super.onResume();
        Log.d(TAG,"onResume:被调用");
    }

    @Override
    protected void onPause(){
        super.onPause();
        Log.d(TAG,"onPause:被调用");
    }

    @Override
    protected void onStop(){
        super.onStop();
        Log.d(TAG,"onStop:被调用");
    }

    @Override
    protected void onDestroy(){
        super.onDestroy();
        Log.d(TAG,"onDestroy:被调用");
    }



//    private void checkAnswer(boolean userAnswer){
//            final boolean correctAnswer = questions.get(currentIndex).isAnswer();
//            int msgId = correctAnswer == userAnswer ?
//                    R.string.correct_toast:R.string.incorrect_toast;
//            Toast.makeText(this, msgId, Toast.LENGTH_SHORT).show();
//        }


    private void updateQuestion() {
        int resId = questions.get(currentIndex).getTextResId();
        binding.tvAus.setText(resId);
    }
    @SuppressLint("NonConstantResourceId")
    @Override
    public void onClick(View view) {
        final Intent intent = new Intent(MainActivity.this, MainActivity2.class);

        switch (view.getId()) {
            case R.id.btn_cheat:
                Log.d("`11212121",""+questions.get(currentIndex).isAnswer());
                intent.putExtra("anserData",questions.get(currentIndex).isAnswer());
                startActivity(intent);
                break;

            case R.id.btn_true:
                if (questions.get(currentIndex).isAnswer()){
                    Toast.makeText(MainActivity.this,"cheating is true",Toast.LENGTH_SHORT).show();
                }else{
                    Toast.makeText(MainActivity.this,"cheating is wrong.",Toast.LENGTH_SHORT).show();
                }
                break;

            case R.id.btn_false:
                if (!questions.get(currentIndex).isAnswer()){
                    Toast.makeText(MainActivity.this,"cheating is true",Toast.LENGTH_SHORT).show();
                }else{
                    Toast.makeText(MainActivity.this,"cheating is wrong.",Toast.LENGTH_SHORT).show();
                }
                break;
        }


}}